home *** CD-ROM | disk | FTP | other *** search
- -------------------------------------------------------
- -- An Interactive Program to Help You Learn Euphoria --
- -------------------------------------------------------
- include get.e
- include graphics.e
-
- constant NTRYS = 3
-
- procedure get_answer(object correct)
- sequence answer
- atom t
-
- for i = 1 to NTRYS do
- answer = get(0)
- if answer[1] = GET_SUCCESS then
- if compare(answer[2], correct) = 0 then
- puts(1, "Correct!\n\n")
- sound(2000)
- t = time()
- while time() < t+0.1 do
- end while
- sound(0)
- return
- elsif i < NTRYS then
- puts(1, "Try again\n")
- sound(200)
- t = time()
- while time() < t+0.4 do
- end while
- sound(0)
- end if
- else
- puts(1, "syntax error - a Euphoria object is expected\n")
- while getc(0) != '\n' do
- end while
- end if
- end for
- puts(1, "The correct answer was: ")
- print(1, correct)
- puts(1, '\n')
- end procedure
-
- procedure part1()
- -- evaluating simple expressions
- object x, y
-
- puts(1, "Please evaluate the following Euphoria expressions\n")
- puts(1, "You have 3 guesses - control-c to quit\n\n")
-
- x = rand(10)
- y = rand(10)
- printf(1, "%d + %d\n", {x, y})
- get_answer(x + y)
-
- x = rand(repeat(10, 3))
- y = rand(10)
- print(1, x)
- puts(1, " * ")
- print(1, y)
- puts(1, '\n')
- get_answer(x * y)
-
- x = rand(repeat(10, 4)) - 5
- y = rand(repeat(10, 4)) - 5
- print(1, x)
- puts(1, " > ")
- print(1, y)
- puts(1, '\n')
- get_answer(x > y)
-
- x = rand(20)
- y = rand(5)
- puts(1, "repeat(")
- print(1, x)
- puts(1, ", ")
- print(1, y)
- puts(1, ")\n")
- get_answer(repeat(x, y))
-
- x = rand(repeat(25, 3)) + 'a'
- y = rand(repeat(25, 2)) + 'a'
- printf(1, "\"%s\" & \"%s\"\n", {x, y})
- get_answer(x & y)
-
- puts(1, "append(")
- print(1, x)
- puts(1, ", ")
- print(1, y)
- puts(1, ")\n")
- get_answer(append(x, y))
-
- puts(1, "what will the value of x be\n")
- puts(1, "after executing the following statements?\n")
- puts(1, "x = ")
- x = rand({10, 10, {10, 10, 10, 10}, 20})
- print(1, x)
- y = rand({20, 20, 20, 20, 20})
- puts(1, "\ny = ")
- print(1, y)
- puts(1, "\nx[3][2..3] = y[4..5]\n")
- x[3][2..3] = y[4..5]
- get_answer(x)
- end procedure
-
- procedure test_program(sequence file_name, object correct)
- -- test a program
- integer lout
- sequence answer
-
- for i = 1 to NTRYS do
- system("ed " & file_name, 0)
- while 1 do
- system("del ex.err > NUL", 0)
- system("ex " & file_name & " > learn.out", 0)
- lout = open("ex.err", "r")
- if lout = -1 then
- exit
- else
- close(lout)
- system("ed", 0)
- end if
- end while
- lout = open("learn.out", "r")
- answer = get(lout)
- if answer[1] = GET_SUCCESS then
- if compare(correct, answer[2]) = 0 then
- puts(1, "Congratulations, your program worked!\n")
- return
- end if
- end if
- puts(1, "Sorry, your program is not correct - \n")
- puts(1, "your output is:\n")
- system("type learn.out", 1)
- if i < NTRYS then
- puts(1, "\nTry again ...\n")
- end if
- end for
- end procedure
-
- integer user_prog
-
- procedure comment(sequence line)
- puts(user_prog, "-- " & line & '\n')
- end procedure
-
- procedure prog_line(sequence line)
- puts(user_prog, line & '\n')
- end procedure
-
- procedure part2a()
- -- programming
- sequence correct
-
- user_prog = open("work1.ex", "w")
- comment("Program #1")
- comment("You are now in the Euphoria editor.")
- comment("Write a program that will print a sequence")
- comment("containing the integers from 1 to 100.")
- comment("Your output should look like:")
- comment("{1, 2, 3, 4, ..., 99, 100}")
- comment("When you are finished, save your program with Esc s Enter\n")
- close(user_prog)
-
- correct = {}
- for i = 1 to 100 do
- correct = append(correct, i)
- end for
- test_program("work1.ex", correct)
- end procedure
-
- procedure part2b()
- user_prog = open("work2.ex", "w")
- comment("Program #2")
- comment("You are now in the Euphoria editor.")
- comment("Write a type-function called special that will only allow")
- comment("a variable to be 3, 17, 52 or 99.")
- comment("Remember that type-functions should return non-zero (TRUE) when")
- comment("an object belongs to the type, and 0 (FALSE) when it does not.")
- comment("The loop at the bottom will call this type function to")
- comment("test it.")
- comment("When you are finished, save your program with Esc s Enter\n")
-
- prog_line("type special(integer x)")
- comment("now complete it ...")
- prog_line("\n\n\n")
- comment("code to test your type ...")
- prog_line("sequence good")
- prog_line("good = {}")
- prog_line("for i = -10000 to 10000 do")
- prog_line(" if special(i) then")
- prog_line(" good = append(good, i)")
- prog_line(" end if")
- prog_line("end for")
- prog_line("? good")
- close(user_prog)
- test_program("work2.ex", {3, 17, 52, 99})
- end procedure
-
- procedure part2c()
- user_prog = open("work3.ex", "w")
- comment("Program #3")
- comment("You are in the Euphoria editor.")
- comment("Write a program that will get() a sequence from")
- comment("a file called \"learn.dat\" in this directory.")
- comment("It will then print the company name with the highest score.")
- comment("The input data is a sequence of 2-element sequences like:")
- comment("{")
- comment(" {\"IBM\", 50},")
- comment(" {\"Microsoft\", 62},")
- comment(" ... etc ...")
- comment("}")
- comment("As you can see the first element is the name and the second is")
- comment("the score.")
- comment("Print the name using: printf(1, \"\\\"%s\\\"\", {name})")
- comment("Type Esc s Enter to save your file, when you are ready.")
- comment("Hints: Remember to include get.e")
- comment(" Remember that get returns a 2-element sequence:")
- comment(" {status, object-read}")
- comment(" A single call to get will read in the whole list.")
- comment(" The scores are from 0 to 100.")
- comment(" If you put your code inside a procedure,")
- comment(" remember to call it!")
- comment(" You shouldn't need more than about 25 lines of code.\n")
- close(user_prog)
- test_program("work3.ex < learn.dat", "Rapid Deployment Software")
- end procedure
-
- -- comment out any of these to skip them:
- part1() -- quick questions
- part2a() -- program 1
- part2b() -- program 2
- part2c() -- program 3
-
-